package mvp.com.customview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    CustomProgressBar bar;

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            bar.setProgress(msg.arg1);
            bar.setProgressText(msg.arg2 + "%");
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bar = (CustomProgressBar) findViewById(R.id.progressbar);

        new Thread(myRun).start();

    }

    Runnable myRun = new Runnable() {

        int p = 0;

        @Override
        public void run() {
            synchronized (this) {
                // 循环执行
                while (true) {
                    if (p > 100) {
                        p = 0;
                    }
                    Message msg = Message.obtain();
                    msg.arg1 = p;
                    msg.arg2 = p;
                    mHandler.sendMessage(msg);
                    p++;
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };
}
